<%@ page import="com.zzty.entity.User" %>
<%@ page import="com.zzty.common.DBUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>今日竞赛</title>
    <link rel="stylesheet" href="../css/style.default.css" type="text/css"/>
    <script type="text/javascript" src="../js/jquery-1.7.min.js"></script>
    <script type="text/javascript" src="../js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="../js/jquery.cookie.js"></script>
    <script type="text/javascript" src="../js/jquery.alerts.js"></script>
    <script type="text/javascript" src="../js/jquery.uniform.min.js"></script>
    <script type="text/javascript" src="../js/general.js"></script>
    <script type="text/javascript" src="../js/blog.js"></script>
    <script></script>

    <!--[if lte IE 8]>
    <script language="javascript" type="text/javascript" src="../js/plugins/excanvas.min.js"></script><![endif]-->
    <!--[if IE 9]>
    <link rel="stylesheet" media="screen" href="../css/style.ie9.css"/>
    <![endif]-->
    <!--[if IE 8]>
    <link rel="stylesheet" media="screen" href="../css/style.ie8.css"/>
    <![endif]-->
    <!--[if lt IE 9]>
    <!--script src="../js/plugins/css3-mediaqueries.js"-->
    <script src="../js/ajaxAPI.js"></script>

    <script type="text/javascript">
        var path = '<%=request.getContextPath()%>';
        var date = new Date();
        date.getHours();
        date.getMinutes();
        date.getSeconds();
        var nowDate = date.toLocaleDateString();
        var fullDate=date.toLocaleString();
        var questionId = null;
        var selfExamId=null;


        function init() {
            ajaxCall("POST", path + "/SelfExamServlet?method=checkHomework&nowDate=" + nowDate, checkLogin);
        }

        function checkLogin(result) {
            var obj = JSON.parse(result);
            selfExamId = obj.id;
            questionId = obj.questionId;
          
            if (obj.questionId == null || selfExamId==null ) {
                alert("今日没有作业...");
            } else {
                ajaxCall("POST", path + "/SelfExamServlet?method=checkCommit&nowDate=" + nowDate + "&selfExamId=" + selfExamId, checkCommit);
            }

        }
        function checkCommit(res) {
            var obj=JSON.parse(res);
            if(obj==true){
                alert("作业已提交");
                var div = document.getElementById("divMessage");
                div.style.display = "block";
            }else {
                ajaxCall("POST", path + "/SelfExamServlet?method=checkLogin&nowDate=" + nowDate + "&selfExamId=" + selfExamId, getQuestion);

            }
        }
        function getQuestion(res) {
            var obj=JSON.parse(res);
            if(obj==true){
                //之前已登录
                alert("请继续答题...");
                ajaxCall("POST", path + "/SelfExamServlet?method=secondStartExam&nowDate=" + fullDate+
                    "&questionId="+questionId+"&selfExamId="+selfExamId, setQuestion);

            }else{
                //这是第一次登陆
                ajaxCall("POST", path + "/SelfExamServlet?method=startExam&nowDate=" + fullDate+
                    "&questionId="+questionId+"&selfExamId="+selfExamId, setQuestion);

            }
        }


        function setQuestion(result) {
           var question = JSON.parse(result);
            var questions = document.getElementById("questions");
                alert("请开始答题：");
                var div = document.getElementById("divSub");
                div.style.display = "block";
                questions.value = question;
                time_now();
                time_fun();



        }

        function time_now() {
            var dNow = new Date();
            document.getElementById("date").innerHTML = "开始时间:" + two_char(dNow.getHours()) + ":" + two_char(dNow.getMinutes()) + ":" + two_char(dNow.getSeconds());
        }

        function time_fun() {
            var sec = 0;
            setInterval(function () {
                sec++;
                var date = new Date(0, 0)
                date.setSeconds(sec);
                var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds();
                document.getElementById("mytime").innerText = "已用时间:" + two_char(h) + ":" + two_char(m) + ":" + two_char(s);
            }, 1000);
        }

        function two_char(n) {
            return n >= 10 ? n : "0" + n;
        }
        function checkForm() {
           ajaxCall("POST", path + "/SelfExamUploadServlet",ss);
            document.addForm.submit();

        }
        function ss(res) {
            var obj=JSON.parse(res);
            if(obj==true){
  //              window.location.href="todayExam.jsp?backurl="+window.location.href;
                alert("提交成功！");
            }else {
                alert("提交失败！");
            }


        }


    </script>
    <script src="../js/change.js"></script>
    <![endif]-->
</head>

<body class="withvernav" onload="init()">
<div class="bodywrapper">
    <div class="topheader">
        <div class="left">
            <h1 class="logo"><span>ZZTY</span></h1>
            <span class="slogan">自在天原学习系统</span>


            <br clear="all"/>

        </div><!--left-->
        <% User user = (User) session.getAttribute("user");
        %>
        <div class="right">
            <!--<div class="notification">
                <a class="count" href="ajax/notifications.html"><span>9</span></a>
            </div>-->
            <div class="userinfo">
                <img src="../images/thumbs/avatar.png" alt=""/>
                <span><%=user.getName()%></span>
            </div><!--userinfo-->

            <div class="userinfodrop">
                <div class="avatar">
                    <a href=""><img src="../images/thumbs/avatarbig.png" alt=""/></a>
                </div><!--avatar-->
                <div class="userdata">

                    <ul>
                        <li>名字:<%=user.getName()%>
                        </li>
                        <li>班级:java<%=user.getClassId()%>班</li>
                        <li>邮箱:<%=user.getEmail()%>
                        </li>
                        <li>类型:<%=user.getType()%>
                        </li>
                        <li><a href="change.jsp">修改信息</a></li>
                        <li><a href="../login.jsp">退出</a></li>
                    </ul>
                </div><!--userdata-->
            </div><!--userinfodrop-->
        </div><!--right-->
    </div><!--topheader-->


    <div class="header">
        <ul class="headermenu">
            <li><a href="Smain.jsp"><span class="icon icon-flatscreen"></span>首页</a></li>
            <li><a href="todayTeamExam.jsp"><span class="icon icon-pencil"></span>结对</a></li>
            <li class="current"><a href="todayExam.jsp"><span class="icon icon-message"></span>竞赛</a></li>
            <li><a href="share1.jsp"><span class="icon icon-chart"></span>课堂分享</a></li>
        </ul>


    </div><!--header-->

    <div class="vernav">
        <ul>
            <li class="current"><a href="todayExam.jsp" class="editor">今日竞赛</a></li>
            <li><a href="historyQuestion.jsp">历史题目</a></li>
            <li><a href="myCode.jsp">我的代码</a></li>

        </ul>
        <a class="togglemenu"></a>
    </div><!--leftmenu-->

    <div class="centercontent">

        <div class="pageheader">
            <h1 class="pagetitle">今日竞赛</h1>


        </div><!--pageheader-->

        <div id="contentwrapper" class="contentwrapper">
            <!--add you page at here-->
            <!--add you page at here-->

            <!--提交作业用这个-->
            <li id="date">开始时间:00:00:00</li>
            <li id="mytime">已用时间:00:00:00</li>

            <li>今日作业：
                <div>
                        <textarea id="questions" name="elm1" rows="15" cols="80" style="width: 80%" class="tinymce">


                        </textarea>
                </div>
            </li>


            <div class="two_third">

                <br/>
                <div id="divSub" style="display: none ">
                    <form method="post" action="../SelfExamUploadServlet" enctype="multipart/form-data">
                        选择一个文件:
                        <input type="file" name="uploadFile"/>
                        <br/><br/>
                        <input type="submit" value="上传" onclick="checkForm()"/>
                    </form>
                </div>
                <div id="divMessage" style="display: none">
                    <p>今日竞赛已提交！</p>
                </div>
            </div><!--two_third-->

        </div><!--contentwrapper-->

        <br clear="all"/>

    </div><!-- centercontent -->


</div><!--bodywrapper-->

</body>
</html>
